/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wii.elpro.dao.hb;

import com.wii.elpro.dao.HibernateUtil;
import com.wii.elpro.dao.UserDAOInterface;
import com.wii.elpro.entity.Cabang;
import com.wii.elpro.entity.Departemen;
import com.wii.elpro.entity.User;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author hendro
 */
public class UserDAOImpl extends DAOGenericImpl implements UserDAOInterface {

    public List<User> getUsersByDepartemen(Departemen dep) throws Exception {
        List<User> users = new ArrayList<User>();
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            users = em.createQuery("select usr from User usr where usr.departemen=:input1").setParameter("input1", dep).list();
        } catch (Exception ex) {
            throw ex;
        }
        return users;
    }

    public List<User> getUsersByCabang(Cabang cab) throws Exception {
        List<User> users = new ArrayList<User>();
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            users = em.createQuery("select usr from User usr where usr.cabang=:input1").setParameter("input1", cab).list();
        } catch (Exception ex) {
            throw ex;
        }
        return users;
    }

    public List<User> getUserByDepartmenAndCabang(Departemen dep, Cabang cab) throws Exception {
        List<User> users = new ArrayList<User>();
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            users = em.createQuery("select usr from User usr where usr.departemen=:input1 and usr.cabang=:input2").setParameter("input1", dep).setParameter("input2", cab).list();
        } catch (Exception ex) {
            throw ex;
        }
        return users;
    }

    public boolean usernameIsUsed(String username) throws Exception {
        List<User> users = null;
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            users = em.createQuery("select usr from User usr where usr.username= :input").setParameter("input", username).list();
        } catch (Exception ex) {
            throw ex;
        }
        if (users.isEmpty()) {
            return false;
        } else {
            users = null;
            return true;
        }
    }

    public User login(String username, String password) throws Exception {
        User user = null;
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            user = (User)em.createQuery("select usr from User usr where usr.username= :input1 and usr.password=:input2").setParameter("input1", username).setParameter("input2", password).uniqueResult();
        } catch (Exception ex) {
            throw ex;
        }
        return user;
    }

    public User getUser(long id) throws Exception {
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            return (User)em.load(User.class, id);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<User> getUsers() throws Exception {
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            return em.createQuery("SELECT u FROM User u").list();
        } catch (Exception ex) {
            throw ex;
        }
    }
}
